package com.bluesky.jwf.component.captcha;

/**
 * a wrapper for CaptchasDotNet class.
 * 
 * @author hz00260
 * 
 */
public class CaptchasDotNetService implements CaptchaService {
	private static String USER_NAME = "jackhatedance";
	private static String KEY = "j4kFUXSIasbG4HvZ9J5zPJr4oaVhmN94yNc1lmmV";

	private static CaptchaService service;

	public static CaptchaService getInstance() {
		if (service == null)
			service = new CaptchasDotNetService(USER_NAME, KEY);
		return service;
	}

	private String userName;
	private String key;

	public CaptchasDotNetService(String userName, String key) {
		this.userName = userName;
		this.key = key;
	}

	public String generateRandomString() {
		CaptchasDotNet captchaDotNet = new CaptchasDotNet(userName, key);
		return captchaDotNet.generateRandomString();
	}

	public String getImageUrl(String randomString) {
		CaptchasDotNet captchaDotNet = new CaptchasDotNet(userName, key);

		return captchaDotNet.imageUrl(randomString);
	}

	public String getAudioUrl(String randomString) {
		CaptchasDotNet captchaDotNet = new CaptchasDotNet(userName, key);

		return captchaDotNet.audioUrl(randomString);
	}

	@Override
	public boolean check(String answer, String randomString) {
		CaptchasDotNet captchaDotNet = new CaptchasDotNet(userName, key);

		char c = captchaDotNet.check(answer, randomString);
		return c == 't';
	}

	@Override
	public String getQuestion(String randomString) {
		String s = String.format(
				"<img src=\"%s\"><a href=\"%s\">Voice Mp3</a>",
				getImageUrl(randomString), getAudioUrl(randomString));
		return s;
	}
}
